package org.droidcon.apps.template.provider;

import org.apache.http.MethodNotSupportedException;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class ProfileDatabase extends SQLiteOpenHelper {

	//private SQLiteDatabase database;
	
	public ProfileDatabase(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	private static final String CREATE_TABLE = "create table "
			+ ProfileContract.TABLE + " (" + ProfileContract.KEY_ID
			+ " integer primary key autoincrement, "
			+ ProfileContract.COLUMN_NAME_FIRST + " text not null, "
			+ ProfileContract.COLUMN_NAME_LAST + " text not null, "
			+ ProfileContract.COLUMN_COMPANY_NAME + " text not null, "
			+ ProfileContract.COLUMN_DROIDCON_TICKET_NUMBER
			+ " text not null, "
			+ ProfileContract.COLUMN_MARKET_DEVELOPER_ACCOUNT
			+ " text not null " + ");";
	
	private static final String TAG = ProfileDatabase.class.getCanonicalName();

	@Override
	public void onCreate(SQLiteDatabase database) {
		database.execSQL(CREATE_TABLE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		throw new Error(new MethodNotSupportedException(
				"no upgrade possible yet"));
	}
	
	
	/**
	 * Convenience function to save the users profile from contentvalues.
	 * @param values
	 */
	public void saveProfile(ContentValues values, String id){
		// TODO: check if an ID is given, if so, search the entry and update it
		if (!haveOwnProfile()){
			 getWritableDatabase().insert(ProfileContract.TABLE, null, values);
			 return;
		}
		
		int returnValue = 0;
		returnValue = getWritableDatabase().update(
				ProfileContract.TABLE,
				values, 
				ProfileContract.KEY_ID+"=?" , 
				new String []{id});
		
		Log.d(TAG, String.valueOf(returnValue));
		
	}
	
	/**
	 * Clear the user profile.
	 */
	public void clearProfile(){
		getWritableDatabase().delete(ProfileContract.TABLE, ProfileContract.KEY_ID+"=?" , 
				new String []{String.valueOf("1")});
	}

	/**
	 * Check if there's any user profile stored in the database
	 * @return
	 */
	public boolean haveOwnProfile() {
		Cursor allProfiles = getReadableDatabase().query(ProfileContract.TABLE, null, null, null, null, null, null);
		
		if (allProfiles.moveToFirst()){
			return true;
		}
		
		return false;
	}

	
	/**
	 * Get the users profile. We assume that there is only one entry. 
	 * If there are more, we'll throw an exception as something must be wrong 
	 * with the data.
	 * 
	 * @return
	 */
	public ContentValues getMyProfile() {
		Cursor allProfiles = getReadableDatabase().query(ProfileContract.TABLE, null, null, null, null, null, null);
		
		if (!allProfiles.moveToFirst()){
			return null;
		}
				
		ContentValues values = new ContentValues();
		
		values.put(ProfileContract.COLUMN_NAME_FIRST, allProfiles.getString(allProfiles.getColumnIndex(ProfileContract.COLUMN_NAME_FIRST)));
		
		values.put(ProfileContract.COLUMN_NAME_LAST, allProfiles.getString(allProfiles.getColumnIndex(ProfileContract.COLUMN_NAME_LAST)));
		
		values.put(ProfileContract.COLUMN_NAME_LAST, allProfiles.getString(allProfiles.getColumnIndex(ProfileContract.COLUMN_NAME_LAST)));
		
		values.put(ProfileContract.COLUMN_COMPANY_NAME, allProfiles.getString(allProfiles.getColumnIndex(ProfileContract.COLUMN_COMPANY_NAME)));
	
		values.put(ProfileContract.COLUMN_DROIDCON_TICKET_NUMBER, allProfiles.getString(allProfiles.getColumnIndex(ProfileContract.COLUMN_DROIDCON_TICKET_NUMBER)));
		
		values.put(ProfileContract.COLUMN_MARKET_DEVELOPER_ACCOUNT, allProfiles.getString(allProfiles.getColumnIndex(ProfileContract.COLUMN_MARKET_DEVELOPER_ACCOUNT)));
		
		values.put(ProfileContract.KEY_ID, allProfiles.getString(allProfiles.getColumnIndex(ProfileContract.KEY_ID)));
		
		if(allProfiles.moveToNext()){
			throw new RuntimeException("data incorrect");
		}
		
		return values;
	}
	

}
